package com.casic.modules.jy.controller;

import java.util.List;
import java.util.Map;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.casic.modules.jy.entity.JyRklsEntity;
import com.casic.modules.jy.service.JyRklsService;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.casic.modules.jy.entity.JyXsjbxxEntity;
import com.casic.modules.jy.service.JyXsjbxxService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 学生基本信息表
 * 
 * @author mfc
 * @email 123456789@qq.com
 * @date 2018-09-25 14:55:22
 */
@RestController
@RequestMapping("jyxsjbxx")
public class JyXsjbxxController {
	@Autowired
	private JyXsjbxxService jyXsjbxxService;
	@Autowired
	private JyRklsService jyRklsService;
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
        PageEntity<JyXsjbxxEntity> page=jyXsjbxxService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") String id){
		JyXsjbxxEntity jyXsjbxx = jyXsjbxxService.findById(id);
		return R.ok().put("jyXsjbxx", jyXsjbxx);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody JyXsjbxxEntity jyXsjbxx){
		List<JyXsjbxxEntity> xhList = jyXsjbxxService.selectByXh(jyXsjbxx.getXh());
		if(xhList != null && xhList.size() > 0){
			return R.error("学号在学生信息表中已存在！");
		}
		List<JyRklsEntity> rklsList = jyRklsService.selectByGh(jyXsjbxx.getXh());
		if(rklsList != null && rklsList.size() > 0){
			return R.error("学号在教师信息表中已存在！");
		}
		SysUserEntity user = sysUserService.queryByUserName(jyXsjbxx.getXh());
		if(user != null){
			return R.error("学号在登录用户信息表中已存在！");
		}
		jyXsjbxxService.save(jyXsjbxx);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody JyXsjbxxEntity jyXsjbxx){
		jyXsjbxxService.edit(jyXsjbxx);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] ids){
		jyXsjbxxService.removeByIds(ids);
		return R.ok();
	}


	@PostMapping("/addfromexcel")
	public R addFromExcel(@RequestParam("file") MultipartFile file) {
		String fileName = file.getOriginalFilename();
		try {
			jyXsjbxxService.addFromExcel(fileName, file);
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
		return R.ok();

	}
}
